package com.mobis.search.controller;


import com.mobis.search.pojo.Article;
import com.mobis.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

        @Autowired
    private ArticleSearchService articleSearchService;

        @RequestMapping(method = RequestMethod.POST)
        public Result save(@RequestBody Article article) {
            System.out.println("123123123");
            articleSearchService.add(article);
            return  new Result(true, StatusCode.OK,"添加成功");
        }

        @RequestMapping(value = "/search/{keywords}/{page}/{size}",method = RequestMethod.GET)
        public Result findByTitleOrContent(@PathVariable String keywords,@PathVariable int page,@PathVariable int size){
            Page<Article> pagedata=articleSearchService.findByTitleOrContent(keywords,keywords,page,size);

            PageResult result=new PageResult<Article>(pagedata.getTotalElements(),pagedata.getContent());
            return  new Result(true,StatusCode.OK,"搜索成功",result);
        }


}
